package com.yiren.backstage.config.api;

import android.content.Context;

import com.google.gson.Gson;
import com.yiren.backstage.config.bean.device.CarSignalConfigBean;
import com.yiren.backstage.config.bean.device.DeviceStudyConfigBean;
import com.yiren.backstage.config.bean.device.PracticeCarCheckConfigBean;
import com.yiren.backstage.config.bean.device.SignalSourceConfigBean;
import com.yiren.backstage.config.bean.device.StationParamConfigBean;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.config.bean.device.UserRiskConfigBean;
import com.yiren.backstage.config.enums.ConfigNameEnum;
import com.yiren.backstage.config.services.IConfigService;
import com.yiren.backstage.config.services.impl.ConfigServiceImpl;

/**
 * @author xiaozan
 * @Description 加载参数配置 api
 * @Date 2022/3/18
 */
public class LoadConfigApi {

    private static IConfigService iConfigService = new ConfigServiceImpl();
    private static Gson gson = new Gson();
    private static final String split = "#";

//    <!-----------------------------------------加载设备参数配置----------------------------------------->/

    /**
     * 通过配置名称加载参数
     *
     * @param context
     * @param configName
     * @return
     */
    public static Object loadConfig(Context context, String configName) {
        return iConfigService.getSharedPreferences(context, configName, "").toString();
    }

    /**
     * 加载车辆信号配置(正反转) 对象
     */
    public static CarSignalConfigBean loadCarSignalConfig(Context context) {
        String value = iConfigService.getSharedPreferences(context, ConfigNameEnum.CAR_SIGNAL_CONFIG.toString(), gson.toJson(CarSignalConfigBean.getDefaultCarSignalConfig())).toString();
        CarSignalConfigBean carSignalConfig = gson.fromJson(value, CarSignalConfigBean.class);
        return carSignalConfig;
    }

    /**
     * 加载设备信息配置
     */
    public static DeviceStudyConfigBean loadDeviceStudyConfig(Context context) {
        String value = iConfigService.getSharedPreferences(context, ConfigNameEnum.DEVICE_STUDY_CONFIG.toString(), gson.toJson(DeviceStudyConfigBean.getDefaultDeviceStudyConfig())).toString();
        DeviceStudyConfigBean deviceStudyConfig = gson.fromJson(value, DeviceStudyConfigBean.class);
        return deviceStudyConfig;
    }

    /**
     * 加载练车检测参数配置
     */
    public static PracticeCarCheckConfigBean loadPracticeCarCheckConfig(Context context) {
        String value = iConfigService.getSharedPreferences(context, ConfigNameEnum.PRACTICE_CAR_CHECK_CONFIG.toString(), gson.toJson(PracticeCarCheckConfigBean.getDefaultPracticeCarCheckConfig())).toString();
        PracticeCarCheckConfigBean practiceCarCheckConfig = gson.fromJson(value, PracticeCarCheckConfigBean.class);
        return practiceCarCheckConfig;
    }

    /**
     * 加载信号源配置参数
     */
    public static SignalSourceConfigBean loadSignalSourceConfig(Context context) {
        String value = iConfigService.getSharedPreferences(context, ConfigNameEnum.SIGNAL_SOURCE_CONFIG.toString(), gson.toJson(SignalSourceConfigBean.getDefaultSignalSourceConfig())).toString();
        SignalSourceConfigBean signalSourceConfig = gson.fromJson(value, SignalSourceConfigBean.class);
        return signalSourceConfig;
    }

    /**
     * 加载固定站移动站主机盒参数配置
     */
    public static StationParamConfigBean loadStationParamConfig(Context context) {
        String value = iConfigService.getSharedPreferences(context, ConfigNameEnum.STATION_PARAM_CONFIG.toString(), gson.toJson(StationParamConfigBean.getDefaultStationParamConfig())).toString();
        StationParamConfigBean stationParamConfig = gson.fromJson(value, StationParamConfigBean.class);
        return stationParamConfig;
    }

    /**
     * 加载用户参数配置
     */
    public static UserParamConfigBean loadUserParamConfig(Context context) {
        String value = iConfigService.getSharedPreferences(context, ConfigNameEnum.USER_PARAM_CONFIG.toString(), gson.toJson(UserParamConfigBean.getDefaultUserInfoConfig())).toString();
        UserParamConfigBean userParamConfig = gson.fromJson(value, UserParamConfigBean.class);
        return userParamConfig;
    }

    /**
     * 加载用户风控参数配置
     */
    public static UserRiskConfigBean loadUserRiskConfig(Context context) {
        UserParamConfigBean userParamConfig = loadUserParamConfig(context);
        UserRiskConfigBean userRiskConfig;
        if (null != userParamConfig.getUserInfo()) {
            String value = iConfigService.getSharedPreferences(context, ConfigNameEnum.USER_RISK_CONFIG.toString() + split + userParamConfig.getUserInfo().getId(), gson.toJson(UserRiskConfigBean.getDefaultUserRiskConfig())).toString();
            userRiskConfig = gson.fromJson(value, UserRiskConfigBean.class);
        } else {
            userRiskConfig = UserRiskConfigBean.getDefaultUserRiskConfig();
        }
        return userRiskConfig;
    }

}